#include "eat.h"

static ATCmdValue_t __eat_create_number_value(float value) {
    ATCmdValue_t val;

    val.type = EAT_VALUE_TYPE_NUMBER;
    val.value.number = value;

    return val;
}

static ATCmdValue_t __eat_create_string_value(char* str, uint8_t len) {
    ATCmdValue_t val;
    uint8_t i;

    val.type = EAT_VALUE_TYPE_STRING;
    
    for (i = 0; i < len; i++) {
        if (i >= EAT_MAX_STR_VAL_LEN) {
            break;
        }
        val.value.string.str[i] = *(str+i);
    }

    if (len > EAT_MAX_STR_VAL_LEN) {
        val.value.string.str_len = EAT_MAX_STR_VAL_LEN;
    }
    else {
        val.value.string.str_len = len;
    }

    return val;
}

EAT_Status EAT_IsQueueEmpty(ATCmdQueue_t *queue) {
    
    if (queue->size == 0) {
        return EAT_YES;
    }
    else {
        return EAT_NO;
    }
}

EAT_Status EAT_IsQueueFull(ATCmdQueue_t *queue) {

    if (queue->size >= EAT_QUEUE_LEN) {
        return EAT_YES;
    }
    else {
        return EAT_NO;
    }
}

EAT_Status EAT_PushQueue(ATCmdQueue_t *queue, ATCmd_t atcmd) {
    
    queue->queue[queue->tail] = atcmd;
    queue->tail++;
    queue->tail %= EAT_QUEUE_LEN;

    if (queue->size < EAT_QUEUE_LEN) {
        queue->size++;
    }

    return EAT_OK;
}

EAT_Status EAT_PopQueue(ATCmdQueue_t *queue, ATCmd_t* cmd) {
    ATCmd_t atcmd;

    if (queue->size == 0) {
        return EAT_QUEUE_EMPTY;
    }

    atcmd = queue->queue[queue->head];
    queue->head++;
    queue->head %= EAT_QUEUE_LEN;
    
    *cmd = atcmd;
    return EAT_OK;
}